/*
Copyright (c) 2007-2009, Yusuke Yamamoto
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
 * Neither the name of the Yusuke Yamamoto nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package kdweibo4j;

import java.util.ArrayList;
import java.util.List;

import kdweibo4j.http.Response;
import kdweibo4j.org.json.JSONArray;
import kdweibo4j.org.json.JSONException;
import kdweibo4j.org.json.JSONObject;


/**
 * 社区信息
 * 
 * @author winters_huang
 */
public class Network extends WeiboResponse implements java.io.Serializable {

	public String id = "";
	public String name = "";
	public String logo_url = "";
	public String sub_domain_name = "";
	public String url = "";
	public String status;
	public Network() {

	}

	Network(Response res) throws WeiboException {
		super(res);
		JSONObject json = res.asJSONObject();
		constructJson(json);
	}

	public Network(JSONObject json) throws WeiboException, JSONException {
		constructJson(json);
	}

	public Network(String str) throws WeiboException, JSONException {
		// StatusStream uses this constructor
		super();
		JSONObject json = new JSONObject(str);
		constructJson(json);
	}
	
	/*
	 * 变量存储
	 */
	public void setId(String id){
		this.id = id;
	}
	
	public String getId(){
		return this.id;
	}
	
	public void setName(String name){
		this.name = name;
	}
	
	public String getName(){
		return this.name;
	}
	
	public void setLogo_url(String logo_url){
		this.logo_url = logo_url;
	}
	
	public String getLogo_url(){
		return this.logo_url;
	}
	
	public void setUrl(String url){
		this.url = url;
	}
	
	public String getUrl(){
		return this.url;
	}
	
	public void setSub_domain_name(String sub_domain_name){
		this.sub_domain_name = sub_domain_name;
	}
	
	public String getSub_domain_name(){
		return this.sub_domain_name;
	}
	
	/* modify by Reilost add some field */
	private void constructJson(JSONObject json) throws WeiboException {
		try {
			id = json.getString("id");
			name = json.getString("name");
			logo_url = json.getString("logo_url");
			url = json.getString("url");
			sub_domain_name = json.getString("sub_domain_name");
		} catch (JSONException je) {
			throw new WeiboException(je.getMessage() + ":" + json.toString(),
					je);
		}
	}

	static List<Network> constructNetworks(Response res) throws WeiboException {
		try {
			JSONArray list = res.asJSONArray();
			int size = list.length();
			List<Network> networks = new ArrayList<Network>(size);

			for (int i = 0; i < size; i++) {
				networks.add(new Network(list.getJSONObject(i)));
			}
			return networks;
		} catch (JSONException jsone) {
			throw new WeiboException(jsone);
		} catch (WeiboException te) {
			throw te;
		}

	}
	
	@Override
	public int hashCode() {
		return (int) id.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (null == obj) {
			return false;
		}
		if (this == obj) {
			return true;
		}
		return obj instanceof Network && ((Network) obj).id == this.id;
	}

	@Override
	public String toString() {
		return "Network Name: "+this.getName()+", domain_name:"+this.getSub_domain_name()+"\n";
	}
}
